package com.archive.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.archive.common.ResponseBean;
import com.archive.common.ResultUtil;
import com.archive.common.ToolsUtils;
import com.archive.dto.PageDTO;
import com.archive.entity.SysDictValue;
import com.archive.service.SysDictValueService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典值表 前端控制器
 * </p>
 *
 * @author Janus
 */
@RestController
@RequestMapping({"/dictValue","/home/dictValue"})
public class SysDictValueController {

    @Autowired
    SysDictValueService dictValueService;


    //根据ID获取信息
    @RequestMapping("info/{id}")
    public ResponseBean info(@PathVariable Long id){
        return ResultUtil.successData(dictValueService.getById(id));
    }


    /**
     * 字典添加
     * @param dictValue
     * @return
     */
    @RequestMapping("/add")
    public ResponseBean add(SysDictValue dictValue, HttpSession session) {
        if (dictValue == null) {
            return  ResultUtil.error();
        }
        QueryWrapper<SysDictValue> dictValueQueryWrapper=new QueryWrapper<>();
        dictValueQueryWrapper.eq("dict_code",dictValue.getDictCode())
                .eq("dict_value",dictValue.getDictValue());

        SysDictValue sysDictValue = dictValueService.getOne(dictValueQueryWrapper);
        if(sysDictValue!=null){
            return ResultUtil.error("当前字典编码：值已经存在！");
        }
        dictValueService.save(dictValue);
        return ResultUtil.successData(dictValue);
    }


    /**
     * 系统信息修改
     * @param dictValue
     * @return
     */
    @RequestMapping("/edit")
    public ResponseBean updateById(SysDictValue dictValue, HttpSession session) {
        if (dictValue == null) {
            return  ResultUtil.error();
        }
        QueryWrapper<SysDictValue> dictValueQueryWrapper=new QueryWrapper<>();
        dictValueQueryWrapper.eq("dict_code",dictValue.getDictCode())
                .eq("dict_value",dictValue.getDictValue())
                .ne("id",dictValue.getId());

        SysDictValue sysDictValue = dictValueService.getOne(dictValueQueryWrapper);
        if(sysDictValue!=null){
            return ResultUtil.error("当前字典编码：值已经存在！");
        }
        dictValue.setUpdateTime(LocalDateTime.now());
        dictValueService.updateById(dictValue);
        return ResultUtil.successData(dictValue);
    }


    /**
     * 分页列表查询
     * @param queryParam 查询参数
     * @return
     */
    @ApiOperation(value = "查询系统信息列表")
    @RequestMapping(value = "/list")
    public ResponseBean getList(PageDTO pageDTO, SysDictValue queryParam , HttpSession session) {
        QueryWrapper<SysDictValue> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(queryParam.getDictCode()),"dict_code",queryParam.getDictCode());
        queryWrapper.like(!StringUtils.isEmpty(queryParam.getName()),"name",queryParam.getName());
        queryWrapper.orderBy(true,pageDTO.getIsAsc().equals("asc"), ToolsUtils.camelToUnderline(pageDTO.getOrderByColumn()));
        IPage<SysDictValue> indexPage = new Page<>(pageDTO.getPageNum(),pageDTO.getPageSize());
        IPage<SysDictValue> listPage = dictValueService.page(indexPage, queryWrapper);
        //获取分页信息
        Map pageInfo = ToolsUtils.wrapperPageInfo(listPage);
        return ResultUtil.successData(pageInfo);
    }

    /**
     * 系统信息删除
     * @param idList
     * @return
     */
    @RequestMapping("delete")
    public ResponseBean delete(@RequestParam("idList") List<Long> idList){
        if(idList==null||idList.isEmpty()){
            return ResultUtil.error("ID不合法！");
        }
        dictValueService.removeByIds(idList);
        return ResultUtil.success();
    }
    /**
     * 分页列表查询
     * @return
     */
    @ApiOperation(value = "")
    @RequestMapping(value = "/queryBy/{enName}")
    public ResponseBean queryBy(@PathVariable("enName") String enName) {
        QueryWrapper<SysDictValue> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("en_name",enName);
        List<SysDictValue> list = dictValueService.list(queryWrapper);
        //获取分页信息
        return ResultUtil.successData(list);
    }

}
